package com.binest.invitationLink.service.impl;

import java.util.List;

import com.binest.common.core.domain.entity.SysUser;
import com.binest.common.core.domain.model.LoginUser;
import com.binest.common.utils.DateUtils;
import com.binest.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.binest.invitationLink.mapper.InvitationLinkMapper;
import com.binest.invitationLink.domain.InvitationLink;
import com.binest.invitationLink.service.IInvitationLinkService;

import static com.binest.common.utils.SecurityUtils.getLoginUser;

/**
 * 邀请链接Service业务层处理
 * 
 * @author wwd
 * @date 2024-02-03
 */
@Service
public class InvitationLinkServiceImpl implements IInvitationLinkService 
{
    @Autowired
    private InvitationLinkMapper invitationLinkMapper;

    /**
     * 查询邀请链接
     * 
     * @param id 邀请链接主键
     * @return 邀请链接
     */
    @Override
    public InvitationLink selectInvitationLinkById(String id)
    {
        return invitationLinkMapper.selectInvitationLinkById(id);
    }

    /**
     * 查询邀请链接列表
     * 
     * @param invitationLink 邀请链接
     * @return 邀请链接
     */
    @Override
    public List<InvitationLink> selectInvitationLinkList(InvitationLink invitationLink)
    {
        return invitationLinkMapper.selectInvitationLinkList(invitationLink);
    }

    /**
     * 新增邀请链接
     * 
     * @param invitationLink 邀请链接
     * @return 结果
     */
    @Override
    public int insertInvitationLink(InvitationLink invitationLink)
    {

        //获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser currentUser = loginUser.getUser();

        invitationLink.setCreateTime(DateUtils.getNowDate());
        invitationLink.setId(IdUtils.simpleUUID());
        invitationLink.setCreateBy(currentUser.getUserId().toString());


        return invitationLinkMapper.insertInvitationLink(invitationLink);
    }

    /**
     * 修改邀请链接
     * 
     * @param invitationLink 邀请链接
     * @return 结果
     */
    @Override
    public int updateInvitationLink(InvitationLink invitationLink)
    {

        LoginUser loginUser = getLoginUser();
        SysUser currentUser = loginUser.getUser();
        invitationLink.setUpdateBy(currentUser.getUserId().toString());
        invitationLink.setUpdateTime(DateUtils.getNowDate());

        return invitationLinkMapper.updateInvitationLink(invitationLink);
    }

    @Override
    public int updateLink(InvitationLink invitationLink) {
        int res=0;
        if(invitationLink.getId()==null){
            res=   insertInvitationLink(invitationLink);
        }else{
            res=   updateInvitationLink(invitationLink);
        }
        return res;
    }

    /**
     * 批量删除邀请链接
     * 
     * @param ids 需要删除的邀请链接主键
     * @return 结果
     */
    @Override
    public int deleteInvitationLinkByIds(String[] ids)
    {
        return invitationLinkMapper.deleteInvitationLinkByIds(ids);
    }

    /**
     * 删除邀请链接信息
     * 
     * @param id 邀请链接主键
     * @return 结果
     */
    @Override
    public int deleteInvitationLinkById(String id)
    {
        return invitationLinkMapper.deleteInvitationLinkById(id);
    }

    @Override
    public InvitationLink selectInvitationLinkByCreateBy() {
        //获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser currentUser = loginUser.getUser();
        String createBy  =currentUser.getUserId().toString();
        InvitationLink res=invitationLinkMapper.selectInvitationLinkByCreateBy(createBy);
        return res;
    }
}
